﻿function ECommerceOrdersViewModel(context) {
    var self = this;

    var spinner = new SpinnerModel();

    var orders = ko.observableArray([]);
    
    var orderRoute = context.spaRuntime.findRouteByName('order');
    
    function reload() {
        var api = context.api();
        spinner.enter();
        api.orders.list(function (data) {
            spinner.leave();
            var res = [];
            for (var i = 0; i < data.length; i++) {
                res.push(new ECommerceOrder(data[i]));
            }
            orders(res);
        }, function () {
            spinner.leave();
        });
    }

    function getOrderUrl(order) {
        return orderRoute.formatHash({
            orderId: order.id()
        });
    }
    
    self.spinner = spinner;
    self.reload = reload;
    self.orders = orders;
    self.getOrderUrl = getOrderUrl;
}